package demo.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/6-08-06-10:14
 * @Description：demo.reflect
 */
public class person_use {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("demo.reflect.person");

        Constructor constructor = c.getDeclaredConstructor(String.class, int.class);
        Object per = constructor.newInstance("hjy",18);
        System.out.println(per);

        System.out.println("------------------------------");
        Method method = c.getDeclaredMethod("setName", String.class);
        method.invoke(per,"hhh");
        System.out.println(per);

        System.out.println("----------------------------------");
        Field field = c.getDeclaredField("age");
        field.setAccessible(true);
        field.set(per,1);
        System.out.println(per);
    }
}
